package com.cdwm.mrp.controller.api;

import com.cdwm.mrp.common.tools.LayuiResponse;
import com.cdwm.mrp.common.tools.ResultVO;
import com.cdwm.mrp.domain.po.Host;
import com.cdwm.mrp.service.HostService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * @version v1.0
 * @Author: lyw
 * @Date: 2021/8/19
 */
@Api(tags = "主机信息管理")
@RestController
@RequestMapping("/api")
public class HostController {
    @Autowired
    private HostService hostService;



    @ApiOperation("保存主机信息")
    @PostMapping("/host/save")
    public ResultVO<?> saveHost(@RequestBody Host host) {
        return hostService.save(host) ? ResultVO.ok(): ResultVO.fail();
    }

    @ApiOperation("删除主机信息")
    @PostMapping("/host/delete/{id}")
    public ResultVO<?> deleteHost(@PathVariable Long id) {

        return hostService.removeById(id) ? ResultVO.ok(): ResultVO.fail();
    }


    /**
     * 批量删除用户
     * @param id
     * @return
     */
    @PostMapping("/host/batchRemove/{id}")
    public ResultVO<?> batchRemove(@PathVariable  Long[] id){

        boolean removeStatus = hostService.removeByIds(Arrays.asList(id));

        if (removeStatus){
            return  ResultVO.ok();
        }
        return  ResultVO.fail();
    }

    @ApiOperation("修改主机信息")
    @PostMapping("/host/update")
    public ResultVO<?> updateHost(@RequestBody Host host) {
        return hostService.updateById(host) ? ResultVO.ok(): ResultVO.fail();
    }


    @ApiOperation("查询主机信息")
    @GetMapping("/host/list")
    public LayuiResponse<?> listhost(
            Host host,
            @RequestParam(required = false,defaultValue = "1") Integer page,
            @RequestParam(required = false,defaultValue = "5") Integer limit
    ) {
        return hostService.getHostList(host,page,limit);
    }

    @ApiOperation("查询主机信息名")
    @GetMapping("/host/listHostName")
    public ResultVO<?> listHostName() {
        return ResultVO.ok(hostService.list());
    }
}
